package com.yz.xyt.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yz.xyt.constant.HttpStatus;
import com.yz.xyt.domain.Clazz;
import com.yz.xyt.domain.ResponseResult;
import com.yz.xyt.domain.vo.ClazzImportVo;
import com.yz.xyt.domain.vo.ClazzVo;
import com.yz.xyt.domain.vo.StudentVo;
import com.yz.xyt.mapper.ClazzMapper;
import com.yz.xyt.service.IClazzService;
import com.yz.xyt.service.IMajorService;
import com.yz.xyt.service.IStaffService;
import com.yz.xyt.service.IStudentService;
import com.yz.xyt.utils.DateUtils;
import com.yz.xyt.utils.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class ClazzServiceImpl implements IClazzService {

    @Autowired
    ClazzMapper clazzMapper;

    @Autowired
    IStaffService staffService;

    @Autowired
    IMajorService majorService;

    @Autowired
    IStudentService studentService;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return clazzMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public int insert(Clazz record) {
        return clazzMapper.insert(record);
    }

    @Transactional
    @Override
    public int insertSelective(Clazz record) {
        return clazzMapper.insertSelective(record);
    }

    @Override
    public Clazz selectByPrimaryKey(Integer id) {
        return clazzMapper.selectByPrimaryKey(id);
    }

    @Override
    public Clazz selectByClazzNumber(String clazzNumber) {
        return clazzMapper.selectByClazzNumber(clazzNumber);
    }

    @Transactional
    @Override
    public int updateByPrimaryKeySelective(Clazz record) {
        return clazzMapper.updateByPrimaryKeySelective(record);
    }

    @Transactional
    @Override
    public int updateByClazzNumberSelective(Clazz record) {
        return clazzMapper.updateByClazzNumberSelective(record);
    }

    @Transactional
    @Override
    public int updateByPrimaryKey(Clazz record) {
        return clazzMapper.updateByPrimaryKey(record);
    }

    /**
     * 增量添加
     * @param list
     */
    @Transactional
    public void syncAddClazz(List<Clazz> list){
        //过滤重复数据
        List<Clazz> clazzList = new ArrayList<>();
        list.stream().forEach(clazz -> {
            Clazz record = selectByClazzNumber(clazz.getClazzNumber());
            if(record == null){
                clazzList.add(clazz);
            }
        });
        //新增班级信息
        clazzList.stream().peek(clazz -> {
            clazz.setCreateBy(SecurityUtils.getUsername());
            clazz.setCreateDate(DateUtils.getNowDate());
        }).collect(Collectors.toList()).stream().forEach(this::insertSelective);

    }

    /**
     * 同步更新
     * @param list
     */
    @Transactional
    @Override
    public void syncUpdateClazz(List<Clazz> list) {
        //更新班级信息
        list.stream().peek(clazz -> {
            clazz.setUpdateBy(SecurityUtils.getUsername());
            clazz.setUpdateDate(DateUtils.getNowDate());
        }).collect(Collectors.toList()).forEach(this::updateByClazzNumberSelective);

    }

    /**
     * 同步删除
     * @param list
     */
    @Transactional
    @Override
    public void syncDeleteClazz(List<Clazz> list) {
        list.stream().forEach(clazz -> {
            clazzMapper.deleteByClazzNumber(clazz.getClazzNumber());
        });
    }

    /**
     * 转换clazz
     * @param clazzSet
     * @return
     */
    @Override
    public List<Clazz> convertClazz(Set<ClazzImportVo> clazzSet) {

        List<Clazz> clazzList = new ArrayList<>();
        clazzSet.stream().forEach(c->{
            //取班级名称hash值生成班级编码
            String classNumber = String.valueOf(c.getName().hashCode()&Integer.MAX_VALUE);
            //获取辅导员工号
            String staffNumber = staffService.findStaffNumberByName(c.getStaffName());
            //取专业编码
            String majorNumber = majorService.findMajorNumberByName(c.getMajorName());
            if(StringUtils.isNotEmpty(staffNumber) && StringUtils.isNotEmpty(majorNumber)){
                Clazz clazz = new Clazz();
                clazz.setName(c.getName());
                clazz.setClazzNumber(classNumber);
                clazz.setGrade(c.getGrade());
                clazz.setStaffNumber(staffNumber);
                clazz.setMajorNumber(majorNumber);
                clazzList.add(clazz);
            }
        });
        return clazzList;
    }

    @Override
    public String findClazzNumberByName(String clazzName) {
        return clazzMapper.findClazzNumberByName(clazzName);
    }

    /**
     * 分页查询班级
     * @param classVo
     * @return
     */
    @Override
    public PageInfo findClazzForPage(ClazzVo classVo) {
        PageHelper.startPage(classVo.getPageNumber(),classVo.getPageSize());
        List<ClazzVo> studentList = clazzMapper.findClassList(classVo);
        return new PageInfo(studentList);
    }

    /**
     * 查询班级列表
     * @param clazzVo
     * @return
     */
    @Override
    public List<ClazzVo> findClassList(ClazzVo clazzVo) {
        return clazzMapper.findClassList(clazzVo);
    }

    /**
     * 新增班级
     * @param clazz
     * @return
     */
    @Transactional
    public ResponseResult addClazz(Clazz clazz) {
        ResponseResult result = new ResponseResult();
        String clazzName = clazz.getName();
        String clazzNumber = findClazzNumberByName(clazz.getName());
        if (StringUtils.isNotEmpty(clazzNumber)){
            result.setCode(HttpStatus.ERROR);
            result.setMsg("新增失败,该班级已存在");
        }else {
            //以班级名陈hash值作为班级编码
            clazzNumber = String.valueOf(clazzName.hashCode()&Integer.MAX_VALUE);
            clazz.setClazzNumber(clazzNumber);
            clazz.setCreateBy(SecurityUtils.getUsername());
            clazz.setCreateDate(DateUtils.getNowDate());
            insertSelective(clazz);
            result.setCode(HttpStatus.SUCCESS);
            result.setMsg("新增班级成功");
        }
        return result;
    }

    /**
     * 删除班级
     * @param clazzVo
     * @return
     */
    @Transactional
    public ResponseResult deleteClazz(ClazzVo clazzVo) {
        ResponseResult result = new ResponseResult();
        Clazz clazz =  clazzMapper.selectByPrimaryKey(clazzVo.getId());
        if (null != clazz){
            //班级下有学生不能删
            StudentVo studentVo = new StudentVo();
            studentVo.setClazzNumber(clazz.getClazzNumber());

            List<StudentVo> studentVoList = studentService.getStudents(studentVo);
            if (studentVoList.size() > 0){
                result.setCode(HttpStatus.ERROR);
                result.setMsg("不能删除，该班级存在学生");
            }else{
                deleteByPrimaryKey(clazzVo.getId());
                result.setMsg("删除班级成功");
                result.setCode(HttpStatus.SUCCESS);
            }
        }else{
            result.setCode(HttpStatus.ERROR);
            result.setMsg("班级不存在，无法删除");
        }

        return result;
    }

    /**
     * 查询年级列表
     * @return
     */
    @Override
    public List<String> findGradeList() {
        return clazzMapper.findGradeList();
    }
}
